package com.gulimall.order.controller;


import com.gulimall.common.core.domain.Result;
import com.gulimall.order.domain.entity.Order;
import com.gulimall.order.service.IOrderService;
import com.gulimall.order.vo.OrderConfirmVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 坤哥
 * @since 2025-08-25
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Resource
    private IOrderService orderService;
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 订单确认
     * 远程调用用户的收货地址列表
     */
    @GetMapping("/toTrade")
    public Result<OrderConfirmVO> orderConfirm(){
        log.info("订单确认：");
        OrderConfirmVO orderConfirmVO = orderService.orderConfirm();
        return Result.ok(orderConfirmVO);
    }

    /**
     * 下单逻辑，把订单保存到数据库
     */
    @GetMapping("/toOrder")
    public Result toOrder(){
        log.info("下单操作：");
        return orderService.toOrder();
    }

    /**
     * 给队列order.delay.queue发送消息
     */
    @PostMapping("/testSendMsg")
    public Result testSendMsg(){
        Order order = Order.builder().orderSn(UUID.randomUUID().toString()).modifyTime(LocalDateTime.now()).build();
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",order);
        return Result.ok();
    }
}
